package org.NooLab.glue.instances;

import java.util.Vector;

import org.NooLab.glue.components.MessageBoardFactory;
import org.NooLab.glue.components.pkg.TaskPackage;
import org.NooLab.utilities.logging.PrintLog;






public class ResultCompletionTriggers {

	
	Vector<ResultCompletionTrigger> items = new Vector<ResultCompletionTrigger>() ;
	
	MessageBoardFactory mbf ;
	
	PrintLog out;
	
	
	
	public ResultCompletionTriggers( MessageBoardFactory mbf, PrintLog outprn){
	
		this.mbf = mbf;
		out = outprn;
	}
	
	
	
	public int size(){
		return items.size();
	}
	
	public void add( ResultCompletionTrigger rtc){
		items.add(rtc);
	}
	
	public ResultCompletionTrigger getItem(int index){
		return items.get(index);
	}
	
	public void removeItem(int index){
		if ((index>=0) && (index<items.size())){
			items.remove(index);
		}
	}

	public void removeItems( int loIndex, int hiIndex){
	
		int ix=hiIndex; // items.size()-1;
		
		while (ix>=0){
			
			items.remove(ix);
			
			ix--;
		}// ->
		
	}
	
	public void removeItemByTID( long transactionID){
		
		int ix;
		
		ix = indexByID(transactionID);
		removeItem(ix);
	}
			
	public int indexByID( long transactionID){
	
		int index=-1;
		ResultCompletionTrigger rtc;
		
		
		for (int i=0;i<items.size();i++){

			rtc = items.get(i);
			if (rtc.transactionID == transactionID){
				index=i;
				break;
			}
		}
		
		return index;
	}
	

	public int isMatching( TaskPackage localResultPackage ) {
		
		long tid;
		int index=-1;
		ResultCompletionTrigger rtc;
		
		if (items.size()==0){
			index = -3;
		}
		
		tid = localResultPackage.getTransactionID();
		
		for (int i=0;i<items.size();i++){
			rtc = items.get(i);
			index=-1;
			if (rtc.transactionID == tid){
				index=i;
				break;
			}
		}
		
		return index;
	}
	
	
}
